<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Illuminate\Support\Facades\Cache;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *显示商品页
     * @return \Illuminate\Http\Response
     */
    public function index()

    {
        $value = Cache::put('key123', 123, 10);

         echo Cache::get('key123');
        $goods = DB::table('goods')->select(['id','gname','price'])->paginate(3);
        return view('Admin/Products/shop',['goods' => $goods]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *修改页面
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //根据ID查询商品数据，并把数据拿出了
        $oneProduct = DB::table('goods')->select(['gname', 'id','price'])->where('id', $id)->first();

        // dd($oneProduct);
        return view('Admin/Products/show', ['good' => $oneProduct]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
       $bool = DB::table('goods')
        ->where('id', $id)
        ->update([
          'gname' => $request->input('gname'),
          'price' => $request->input('price'),
        ]);

        if ($bool) {
          //修改成功
          return redirect('admin/product')->with('msg', '修改成功');
        }

        return back()->with('errorTip', '修改失败');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($gayquan)
    {
        $bool = DB::table('goods')->where('id', $gayquan)->delete();

        return back()->with('msg', '删除成功');
    }
}
